Project Description¶

In this project, we will build reusable Python functions to support an intergalactic delivery startup. Our goal is to create tools that can format timestamps and break complex code into smaller, reusable parts. These functions will help us estimate arrival times, calculate rocket landing schedules, and keep customers across the galaxy informed about their deliveries. Together, we will use Python to keep space deliveries on time and running smoothly.

Imagine we are starting a space logistics company that uses rockets to deliver important cargo to different colonies on demand. Since we are still a small startup, we handle everything ourselves, including writing the software needed to manage the complicated scheduling and timing across many space colonies.

Before building a full rocket flight planning and logistics system, we will focus on creating core Python functions that work with dates, times, and durations. Using Python’s built-in datetime module, we will create simple and reusable functions to:

  • Format timestamps clearly and consistently.
  • Calculate rocket landing times by combining launch time with travel duration.
  • Find out how many days remain until a delivery deadline, so we can keep customers updated on their shipments.

This project does not use any external data; it focuses on building these essential time-handling functions as the foundation for our future space delivery software.

Let's first define a function called format_date(), which formats a timestamp into a readable datetime string¶

In [1]:
# import the required library
from datetime import datetime, timedelta

# Define the function accepting timestamp and datetime format args
def format_date(timestamp, datetime_format):
    # Convert timestamp arg to datetime object
    datetime_obj = datetime.fromtimestamp(timestamp)  
    # Format datetime_obj to string
    datetime_str = datetime_obj.strftime(datetime_format)
    # Return formatted datetime string
    return datetime_str
In [4]:
format_date(1514665153, "%d-%m-%Y")
Out[4]:
'31-12-2017'

Define a function called calculate_landing_time(), which calculates the estimated landing time.¶

In [2]:
# Define calculate_landing_time function
def calculate_landing_time(rocket_launch_dt, travel_duration):
    # Calculate landing by adding travel_duration to rocket_launch_dt arg
    landing_date = rocket_launch_dt + timedelta(days=travel_duration)
    # Format landing datetime to string
    landing_date_string = landing_date.strftime("%d-%m-%Y") 
    # Return landing date time string 
    return landing_date_string
In [5]:
calculate_landing_time(datetime(2023, 2, 15), 20)
Out[5]:
'07-03-2023'

Define a function named days_until_delivery(), which calculates the days until a package arrives for customers.¶

In [7]:
# Define days_until_delivery function
def days_until_delivery(expected_delivery_dt, current_dt):
    # Calculate the time until delivery
    time_until_delivery = expected_delivery_dt - current_dt
    # Access the date component of the datetime object
    days_until = time_until_delivery.days
    # Return number of days until delivery
    return days_until
In [6]:
days_until_delivery(datetime(2023, 2, 15), datetime(2023, 2, 5))
Out[6]:
10